home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / programm.ing / sources.arc / CUBE6.C < prev    next >
Encoding:
C/C++ Source or Header  |  1994-01-20  |  7.0 KB  |  286 lines

  1. /* Will spin a box in 3d space. 
  2.  
  3.    o ) User can controll x,y,z rotation by keys 1,2 or 3
  4.    o ) Perspective viewing 
  5.    o ) Movement of object in its Object-Coords relative to its origin
  6.        (Default is origin at center of object, so any rotation will
  7.         just make it 'spin' in the x,y,z direction...rather booring
  8.         So change its co-ords w/ respect to its origin to get cool
  9.         tumbling rotations)
  10.    o ) Movement of camera. You can move the camera in the x,y or z
  11.        direction. (ie. You can move to the left or right of the object
  12.                        or you can view it close up or far away )
  13.   
  14.    Jan. 8 ,1994  jeff bilger 
  15.  
  16. */
  17.  
  18.  
  19. #include <math.h>
  20. #include <linea.h>
  21. #include <osbind.h>
  22. int pts[4][2] = { 
  23.     320, 050,
  24.     120, 150,
  25.     520, 150,
  26.     320, 050
  27. };
  28. lineaport *theport;
  29.  
  30.  
  31. #define SCALE 8192L       /* so far 2048 is the upper bounds scale
  32.                               i can get W/O overflow */
  33. #define MF 65536.0
  34. #define BITSH 13
  35. #define PI 205887L
  36. #define SPININC (PI >> 4)
  37. #define addpt(p1,p2,p3) \
  38.         { p3.x = p1.x + p2.x; \
  39.           p3.y = p1.y + p2.y; \
  40.           p4.z = p1.z + p2.z; }
  41.  
  42. #define cosf(x) (cos((double) x / M1))
  43. #define sinf(x) (sin((double) x / M1))
  44.  
  45. typedef struct {long x,y,z;} point3d;
  46.  
  47. #define perx(p) \
  48.         (perspective ? ((p.x/(p.z+pdist))*pdist):p.x)
  49. #define pery(p) \
  50.         (perspective ? ((p.y/(p.z+pdist))*pdist):p.y)
  51.  
  52.  
  53.  
  54. #define NPTS 64
  55. #define NLINES 64
  56. #define NFACES 64
  57.  
  58.  
  59. #define I ( SCALE * 10 )
  60.  
  61. point3d point_[NPTS] =
  62.         { -I,-I,I,   I,-I,I,  I,I,I,  -I,I,I,
  63.            -I,-I,-I, I,-I,-I, I,I,-I, -I,I,-I },
  64.         drawpt1[NPTS],drawpt2[NPTS];
  65.  
  66. long spininc = SPININC >> 2;
  67.  
  68. int drawclr,
  69.     white,
  70.     glasses = 0,
  71.     perspective = 1,
  72.     npts  = 8,
  73.     linefrom[NLINES] = {0,1,2,3,0,4,7,1,5,6,6,5},
  74.     lineto[NLINES]   = {1,2,3,0,4,7,3,5,6,2,7,4},
  75.     from[NLINES]     = {0,1,1,5,4,7,0,3,2,6,1,0},
  76.     to[NLINES]       = {1,2,5,6,7,6,3,7,6,7,0,4},
  77.     nlines = 12;
  78.  
  79. double pdist=200.0;
  80.  
  81. unsigned int pause = 8092;
  82.  
  83. int x_offset=320,y_offset=100,z_offset=1;
  84.  
  85.  
  86. main()
  87. {
  88. register int i,j;
  89. point3d pointi;
  90. int color =1;
  91. long cos_2,sin_2;
  92. char com;
  93.  
  94. cos_2 = (cos( (double) (5.0*3.141596/180.0))) * SCALE;  /* precompute values */
  95. sin_2 = (sin( (double) (5.0*3.141596/180.0))) * SCALE;
  96.  
  97. /*
  98. printf("%ld\n",sin_2);
  99. printf("%ld\n",cos_2);
  100. */
  101.  
  102. printf(" 1-Spin Y  2-Spin X  3-Spin Z\n");
  103. printf(" 4-Perspective ON/OFF\n");
  104. printf(" Move object (x,y,z) -> 7,8,9\n");
  105. printf(" Move view   x/X,y/Y,z/Z\n");
  106. theport = a_init();     
  107.    
  108.  
  109.     theport -> plane0 = 1;
  110.     theport -> plane1 = 0;
  111.     theport -> plane2 = 0;
  112.     theport -> plane3 = 0;
  113.  
  114. /*
  115. printf("1...%ld\n",point_[2].y * cos_2 +point_[2].z * sin_2 );
  116. printf("2...%ld\n",point_[2].y * -sin_2 +point_[2].z * cos_2 );
  117. */
  118.  
  119.  
  120.  for(i=0;i<npts;i++)
  121.    {
  122.    pointi = point_[i];
  123.    point_[i].y =( pointi.y * cos_2  +
  124.                 pointi.z * sin_2)>>BITSH;   /* scale is 2048 or 2^BITSH */
  125.    point_[i].z =( pointi.y * -sin_2 +    /* so divide by scale */
  126.                 pointi.z * cos_2)>>BITSH;
  127.    }
  128. /*printf("1...%ld\n",point_[2].y>>BITSH);
  129. printf("2...%ld\n",point_[2].z>>BITSH);
  130. */
  131.  
  132.  
  133.  
  134.  
  135.  
  136. for(i=0;i<nlines;i++)  /* set up points-to-connect lookup table*/
  137. { drawpt1[i] = point_[linefrom[i]];
  138.   drawpt2[i] = point_[lineto[i]];  
  139. }
  140.  
  141. /*
  142. printf("%ld,%ld,%ld\n",point_[2].x,point_[2].y,point_[2].z);
  143. */
  144.  
  145. while(1)
  146. {
  147.  com = 0x31;
  148.  
  149.  for(i=0;i<npts;i++)
  150.    {
  151.    pointi = point_[i];
  152.    
  153.     if(com == 0x31) {              /* spin y */
  154.                point_[i].x =( pointi.x * cos_2  -
  155.                               pointi.z * sin_2)>> BITSH;
  156.                point_[i].z =( pointi.x * sin_2 +
  157.                               pointi.z * cos_2)>> BITSH;
  158.                     }
  159.     if(com == 0x32) {              /* spin x */
  160.                 point_[i].y =( pointi.y * cos_2  +
  161.                                pointi.z * sin_2)>> BITSH;
  162.                 point_[i].z =( pointi.y * -sin_2 +
  163.                                pointi.z * cos_2)>> BITSH;
  164.                     }
  165.     if(com == 0x33) {              /* spin z */
  166.                 point_[i].x =( pointi.x * cos_2  +
  167.                                pointi.y * sin_2)>> BITSH;
  168.                 point_[i].y =( pointi.y * cos_2  -
  169.                                pointi.x * sin_2)>> BITSH;
  170.                     }
  171.     if(com == 0x37) point_[i].x++;      /* These will move the */
  172.     if(com == 0x38) point_[i].y++;      /* object around in its */
  173.     if(com == 0x39) point_[i].z++;      /* OWN co-ord sys!! Thus */
  174.                                         /* changing these will effect*/
  175.                                         /* how it rotates about the origin */
  176.                                         /* The default is that the origin*/
  177.                                         /* is at the CENTER of the object */
  178.  
  179.    }
  180.  
  181. if( (com > 0x77 && com < 0x7b) || ( com > 0x57 && com < 0x5b) ) 
  182.  {
  183.     for(i=0;i<nlines;i++) draw3dline(drawpt1[i],drawpt2[i],0);
  184.     if(com == 0x78) x_offset++;
  185.     if(com == 0x79) y_offset++;
  186.     if(com == 0x7a) z_offset++;
  187.     if(com == 0x58) x_offset--;
  188.     if(com == 0x59) y_offset--;
  189.     if(com == 0x5a) z_offset--;
  190.    com = 0x36; /* so that code below will execute */
  191.  } 
  192.  
  193. if(com < 0x37)          /* if 1,2,3 chosen, then display it */
  194.   for(i=0;i<nlines;i++)
  195.    { draw3dline(drawpt1[i],drawpt2[i],0);  /*erase */
  196.      draw3dline(drawpt1[i]=point_[linefrom[i]],drawpt2[i]=point_[lineto[i]],color);
  197.      /* draw it */
  198.   }
  199.    
  200. /*
  201. compute_normals();
  202. */
  203.  
  204. if(com == 0x34) perspective = perspective ^ 1;
  205. }
  206.  
  207. }
  208. draw3dline(p1,p2,color)
  209. point3d p1,p2;
  210. int color;
  211. {
  212.  int x1,y1,x2,y2;
  213.  
  214. /*
  215. printf("==> %ld,%ld,%ld\n",p1.x/SCALE,p1.y/SCALE,p1.z/SCALE);
  216.  
  217.  x1 = (( (int)( perx(p1) )*z_offset) +x_offset) / SCALE;
  218.  y1 = (( (int)( pery(p1) )*z_offset) +y_offset) / SCALE;
  219.  x2 = (( (int)( perx(p2) )*z_offset) +x_offset) / SCALE;
  220.  y2 = (( (int)( pery(p2) )*z_offset) +y_offset) / SCALE;
  221. */
  222.  x1 = (p1.z>>12) + x_offset;       /* divide by scale ie: shift left BITSH bits == divide by 2048 */
  223.  y1 = (p1.y>>12) + y_offset;
  224.  x2 = (p2.z>>12) + x_offset;
  225.  y2 = (p2.y>>12) + y_offset;
  226.  
  227.  
  228. /*
  229. printf(" %d,%d  %d,%d\n",x1,y1,x2,y2);
  230. */
  231.  theport -> plane0 = color;
  232.  
  233. a_line(x1,y1,x2,y2); 
  234.  
  235. }
  236.  
  237. mu(a,b)
  238. register long int a,b;
  239. {
  240.  asm
  241.     {
  242.      move.l a,D0
  243.      move.l b,D1
  244.      muls   D0,D1
  245.     }
  246. }
  247. di(a,b)
  248. register long int a,b;
  249. {
  250.  asm
  251.     {
  252.      move.l a,D0
  253.      move.l b,D1
  254.     }
  255. }
  256.  
  257.  
  258.  
  259. compute_normals()
  260. {
  261.  point3d A,B;
  262.  int h;
  263.   double An,Bn,ycomp;
  264.  
  265.  for(h=0;h<6;h++)
  266.    {
  267.     A.x = point_[to[h]].x - point_[from[h]].x;
  268.     A.z = point_[to[h]].z - point_[from[h]].z;
  269.     A.y = point_[to[h]].y - point_[from[h]].y;
  270.     B.x = point_[to[h+1]].x - point_[from[h+1]].x;
  271.     B.z = point_[to[h+1]].z - point_[from[h+1]].z;
  272.     B.y = point_[to[h+1]].y - point_[from[h+1]].y;
  273.  
  274.     An = sqrt(A.x*A.x+A.z*A.z+A.y*A.y);
  275.     Bn = sqrt(B.x*B.x+B.z*B.z+B.y*B.y);
  276.      A.x = A.x / An;
  277.     A.z = A.z / An;
  278.     B.x = B.x / Bn;
  279.     B.z = B.z / Bn;
  280.    
  281.     ycomp = A.z*B.x - A.x*B.z;
  282.     
  283.    }
  284.  
  285. }
  286.